Ext.namespace('UserManager'); 

UserManager.UserItemWindow = function(parent) {

	this.parentWindow = parent;

	this.formPanel = new Ext.FormPanel( {
		labelWidth : 80,
		frame : false,
		header : false,
		border : false,
		bodyStyle : 'padding:10px 10px 0',
		width : 350,
		defaults : {
			width : 230
		},
		defaultType : 'textfield',

		items : [ {
			fieldLabel : 'ID',
			name : 'id',
			xtype : 'hidden'
		}, {
			fieldLabel : 'User ID',
			name : 'userId',
			allowBlank : false
		}, {
			fieldLabel : 'User Name',
			name : 'userName'
		}, {
			fieldLabel : 'Email',
			name : 'email'
		}]
	});

	UserManager.UserItemWindow.superclass.constructor.call(this, {
		title : 'User Information',
		iconCls: 'db-icn-user-info',
		layout : 'fit',
		width : 350,
		height : 170,
		closable : false,
		resizable : false,
		modal: true,

		items : this.formPanel,

		buttons : [ {
			text : 'OK',
			handler : function() {
				var formData = this.formPanel.getForm().getValues();
				var user = Ext.createObject("org.jwos.platform.domain.User");				
				user.userName = formData.userName;
				user.email = formData.email;
								
				//edit
				if(formData.id) {
					user.id = formData.id;					
				} else {
					user.userId = formData.userId;
				}
				
				userManagerRemote.saveUser(user,
		    		function (result,success,error) {
						if(success) {
							this.parentWindow.setUserData(formData);
							this.hide();
						} else {
							Ext.Msg.show({
								title:"User Manager",
								msg: error,
								buttons: Ext.Msg.OK,
								icon: Ext.MessageBox.ERROR
							});
						}
					},
					this
		    	);				
			},
			scope : this
		}, {
			text : 'Cancel',
			handler : function() {
				this.hide();
			},
			scope : this
		}]
	});
};

Ext.extend(UserManager.UserItemWindow, Ext.Window, {

	setValues : function(values) {
		this.formPanel.getForm().setValues(values);
	}

});

UserManager.UserListGrid = function() {

	this.events = {
		"itemdblclick" : true
	};
	
	// create the data store
	this.dataStore = new Ext.data.SimpleStore( {
		fields : [ {
			name : 'id'
		}, {
			name : 'userId'
		}, {
			name : 'userName'
		}, {
			name : 'email'
		}]
	});
	//store.loadData(myData);

	UserManager.UserListGrid.superclass.constructor.call(this, {
		store : this.dataStore,
		columns : [ {
			header : 'id',
			hidden : true,
			sortable : false,
			dataIndex : 'id'
		}, {
			id : 'userId',
			header : "User ID",
			width : 100,
			sortable : true,
			dataIndex : 'userId'
		}, {
			id : 'userName',
			header : "User Name",
			width : 100,
			sortable : true,
			dataIndex : 'userName'
		}, {
			id : 'email',
			header : "Email",
			width : 150,
			sortable : true,
			dataIndex : 'email'
		}],
		stripeRows : true,
		autoExpandColumn : 'email',
		header : false
	});

	this.on('rowdblclick', this.onDblClick, this);
};

Ext.extend(UserManager.UserListGrid, Ext.grid.GridPanel, {

	onDblClick : function(grd, index, e) {
		this.fireEvent("itemdblclick", grd, index, e);
	},

	insertRow : function(data) {
		var store = this.getStore();
		var record = new Ext.data.Record(data);
		store.add([record]);
	},

	updateRow : function(data) {
		var store = this.getStore();
		var record = store.getAt(store.find('id', data.id));
		for (var p in data) {
			record.set(p, data[p]);
		}
	},

	getSelectedData : function() {
		return this.getSelectionModel().getSelections()[0].data;
	}
});

UserManager.UserManager = function(config) {	

	this.userListGrid = new UserManager.UserListGrid();
	this.userItemWindow = new UserManager.UserItemWindow(this);	

	UserManager.UserManager.superclass.constructor.call(this, {
		id : config.id,
		manager: config.manager,
		title : 'User Manager',
		width : 400,
		height : 300,
		layout : 'fit',
		iconCls: 'db-icn-user-manager',
		closable : true,
		minimizable : true,
		maximizable : true,

		tbar : new Ext.Toolbar( {
			items : [{
				text : 'Add',
				iconCls: 'db-icn-user-add',
				handler : function() {
					this.addUser();
				},
				scope : this
			},{
				text : 'Edit',
				iconCls: 'db-icn-user-edit',
				handler : function() {
					this.editUser();
				},
				scope : this
			}]
		}),

		items : {
			layout : 'fit',
			header : false,
			border : false,
			items : this.userListGrid
		}
	});
	
	this.on('render', this.userManagerShow, this);

	this.userListGrid.on("itemdblclick", this.grid_dblclick, this, true);
};

Ext.extend(UserManager.UserManager, Ext.Window, {	

	createWindow: function(config, cls) {
        var desktop = MyDesktop.getDesktop();
        var win = desktop.getWindow(config.id);
        if(!win){
            win = desktop.createWindow(config, cls)
        }    
        return win;
    },
    
    userManagerShow: function() {
    
    	userManagerRemote.queryAllUsers(
    		function (result,success,error) {
				if(success) {
					var userList = result;
					var userRecords = new Array();
			        for(var i=0;i<userList.length;i++) {
			        	userRecords.push(new Ext.data.Record(userList[i]));
			        }
					this.userListGrid.dataStore.add(userRecords);				
				} else {
					alert(error);
				}
			},
			this
    	);        
    },

	grid_dblclick : function(grd, index, e) {
		this.editUser();
	},

	addUser : function() {
		this.userItemWindow.formPanel.getForm().reset();
		this.userItemWindow.show();
		this.userItemWindow.center();
		this.userItemWindow.formPanel.getForm().findField('userId').setDisabled(false);
	},

	editUser : function() {
		if(this.userListGrid.getSelectionModel().getSelections().length>0) {
			this.userItemWindow.show();
			this.userItemWindow.center();
			this.userItemWindow.setValues(this.userListGrid.getSelectedData());
			this.userItemWindow.formPanel.getForm().findField('userId').setDisabled(true);
		} else {
			Ext.Msg.show({
				title:"User Manager",
				msg: "Please select a user to edit.",
				buttons: Ext.Msg.OK,
				icon: Ext.MessageBox.INFO
			});
		}
	},
	
	setUserData : function(values) {
		if(values.id) {
			this.updateUserData(values);
		} else {
			this.insertUserData(values);
		}
	},
	
	insertUserData : function(values) {
		var data = {
			id : values.userId,
			userId : values.userId,
			userName : values.userName,
			email : values.email
		};
		this.userListGrid.insertRow(data);
	},
	
	updateUserData : function(values) {
		var data = {
			id : values.id,
			userName : values.userName,
			email : values.email
		};
		this.userListGrid.updateRow(data);
	}	
});
